package com.example.mapexport.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.services.core.PoiItemV2;
import com.chad.library.adapter4.BaseQuickAdapter;
import com.chad.library.adapter4.viewholder.QuickViewHolder;
import com.example.mapexport.MainActivity;
import com.example.mapexport.R;
import com.example.mapexport.realm.MyPoiItem;
import com.example.mapexport.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PoiAdapter extends BaseQuickAdapter<MyPoiItem, PoiAdapter.MyViewHolder> {


    @NonNull
    @Override
    protected MyViewHolder onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.poi_item, viewGroup, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i, @Nullable MyPoiItem myPoiItem) {
        if(!TextUtils.isEmpty(myPoiItem.getTitle())) {
            myViewHolder.tvTitle.setText(myPoiItem.getTitle());
        } else {
            myViewHolder.tvTitle.setText("");
        }
        myViewHolder.dataList.clear();
        if(!TextUtils.isEmpty(myPoiItem.getTel())) {
            String[] split = myPoiItem.getTel().split(";");
            ArrayList<String> telList = new ArrayList<>(Arrays.asList(split));
            myViewHolder.dataList.addAll(telList);
        }
        myViewHolder.telAdapter.notifyDataSetChanged();
        myViewHolder.tvNumber.setText("" + (i + 1));
    }

    protected class MyViewHolder extends QuickViewHolder {

        TextView tvTitle;
        RecyclerView rvTel;
        TextView tvNumber;
        PoiTelAdapter telAdapter;
        List<String> dataList = new ArrayList<>();
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            tvTitle = itemView.findViewById(R.id.tvTitle);
            rvTel = itemView.findViewById(R.id.rvTel);
            tvNumber = itemView.findViewById(R.id.tvNumber);
            telAdapter = new PoiTelAdapter();
            rvTel.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL,false));
            telAdapter.setItems(dataList);
            rvTel.setAdapter(telAdapter);
            telAdapter.setOnItemClickListener(new OnItemClickListener<String>() {
                @Override
                public void onClick(@NonNull BaseQuickAdapter<String, ?> baseQuickAdapter, @NonNull View view, int i) {
                    if(!TextUtils.isEmpty(baseQuickAdapter.getItem(i))) {
                        Utils.copyTextToClipboard(getContext(),baseQuickAdapter.getItem(i));
                        Toast.makeText(getContext(),"复制成功",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
}
